package com.bo.baby2.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.bo.baby2.config.ReturnResult;
import com.bo.baby2.dao.TLoginLogMapper;
import com.bo.baby2.dao.TUserAccountMapper;
import com.bo.baby2.dao.TUserWalletMapper;
import com.bo.baby2.entity.TLoginLog;
import com.bo.baby2.entity.TUserAccount;
import com.bo.baby2.entity.TUserWallet;
import com.bo.baby2.service.TuserAaccountService;
import com.bo.baby2.tools.MD5;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @program: baby2_p2p
 * @description: userservice实现层
 * @author:波
 * @create: 2020-07-12 13:15
 **/
@Service
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public class TuserAccountServiceImpl implements TuserAaccountService {
    @Resource
    private TUserAccountMapper tUserAccountMapper;  //用户账户dao
    @Resource
    private TUserWalletMapper tUserWalletMapper;    //用户资金dao
    @Resource
    private TLoginLogMapper tLoginLogMapper;    //用户登录日志dao

    @Override
    public Boolean checkUsername(String username) {
        return tUserAccountMapper.checkUsername(username)<1;
    }

    @Override
    public Boolean insertSelective(TUserAccount record) {
        String password = record.getPassword();
        record.setPassword(MD5.encode(password));
        int i = tUserAccountMapper.insertSelective(record);
        boolean flag=false;
        if(i>0){
            TUserWallet userWallet=new TUserWallet();
            userWallet.setAccountId(record.getId());
            userWallet.setAvailableAmount(1000000L);
            int i1 = tUserWalletMapper.insertSelective(userWallet);
            if (i1>0){
                flag=true;
            }
        }
        return flag;
    }

    @Override
    public ReturnResult login(TUserAccount record, String ip) {
        TUserAccount userAccount = tUserAccountMapper.login(record);
        ReturnResult returnResult=null;
        LocalDateTime dateTime=LocalDateTime.now();
        //登录日志表添值
        TLoginLog tLoginLog=new TLoginLog();
        tLoginLog.setUsername(record.getUsername());    //登录名
        tLoginLog.setLoginTime(dateTime);    //登录时间
        tLoginLog.setIp(ip);   //登录ip
        Integer typeId=record.getAccountType();
        tLoginLog.setAccountType(typeId); //登录人员类型
        if(userAccount!=null){
            //判断他是运营人员，还是普通用户
            if (typeId.equals(userAccount.getAccountType())) {
            if(MD5.encode(record.getPassword()).equals(userAccount.getPassword())) {
                userAccount.setLastLoginTime(dateTime);  //设置最后登录时间
                tLoginLog.setLoginResult(1);    //登录是否成功
                userAccount.setPassword("");
                //登录成功之后修改最后登录时间
                tUserAccountMapper.updLastTime(userAccount);
                returnResult = ReturnResult.successResult("登录成功", userAccount);
            }else {
                tLoginLog.setLoginResult(0);
                returnResult = ReturnResult.failResult("登录失败，账号密码不正确");
            }
            //登录类型不符合的时候
            }else {
                tLoginLog.setLoginResult(0);
                if(typeId==1){
                    returnResult=ReturnResult.failResult("登录失败,运营人员登录入口不是在这哦");
                }else if(typeId==2) {
                    returnResult=ReturnResult.failResult("登录失败，非运营人员不能登录哦");
                }
            }
        }else {
            tLoginLog.setLoginResult(0);
            returnResult=ReturnResult.failResult("登录失败，账号不存在");
        }
        tLoginLogMapper.insertSelective(tLoginLog); //新增日志表
        return returnResult;

    }
}
